<?php

namespace app\api\model;

use app\api\model\Common;
use think\Db;

class LanguageModel extends Common
{
    /**
     * 
     */
    public function getList()
    {
        try {
            $map = [
                'is_delete' => 0
            ];
            $result = $this->where($map)->select();
            return $result;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

    /**
     * 
     */
    public function exchangeModel($id)
    {
        try {
            Db::startTrans();
            $_will_run = $this->get($id);
            if (!$_will_run) {
                $this->error = '未找到要修改的数据';
                return false;
            }
            if($_will_run->status == 1)
            {
                $this->error = '替换模型与正在启用模型相同';
                return false;
            }

            $_running = $this->get(['status' => 1]);
            if ($_running) {
                $_running->status = 2;
                $_running->save();
            }

            $_will_run->status = 1;
            $_will_run->save();
            Db::commit();
            return $_will_run;
        } catch (\Exception $e) {
            Db::rollback();
            $this->error = $e->getMessage();
            return false;
        }
    }
}
